/*
Copyright (C) 2015 Ove Kaaven

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/

#include "Scaling.inc"

string name : NAME = "FixVideo";
float scaling : SCALING = 1.0;

string combineTechique : COMBINETECHNIQUE = "FixVideo";

struct VS_OUTPUT_VIDEO
{
	float4 Position		: POSITION;
	float2 CentreUV		: TEXCOORD0;
	float2 Selector		: TEXCOORD1;
};

VS_OUTPUT_VIDEO VS_Video(
			float3 Position : POSITION,
			float2 TexCoord : TEXCOORD0)
{
	VS_OUTPUT_VIDEO Out = (VS_OUTPUT_VIDEO)0;

	Out.Position = mul(float4(Position, 1), WorldViewProjection);
	Out.CentreUV = TexCoord;
//	Out.Selector = TexCoord * SourceDims;
	// why doesn't SourceDims work?
	Out.Selector = TexCoord * float2(512.0f, 512.0f);

	return Out;
}


// This version fills in the black lines with full brightness,
// but since this shader can't tell if this is a video or not,
// the interactive screens will not look very good.
float4 PS_Video1 ( in VS_OUTPUT_VIDEO input ) : COLOR
{
	float2 offset_y = {0, TexelSize.y};
	half4 selector = tex2D(ModuloSampler, input.Selector);
	half4 ret;

	if (selector.y >= 0.5) {
		ret = tex2D(SourceSampler, input.CentreUV);
	}
	else
	{
		// if this is a video, fill black lines using a simple blend
		half4 a = tex2D(SourceSampler, input.CentreUV - offset_y);
		half4 b = tex2D(SourceSampler, input.CentreUV + offset_y);
		ret = (a+b)/2;
	}

	return ret;
}

// This version does a vertical blur, which means the videos
// will have half brightness, but at least lets the interactive
// screens look mostly OK, just a little blurred.
float4 PS_Video2 ( in VS_OUTPUT_VIDEO input ) : COLOR
{
	float2 offset_y = {0, 1.0f/1024.0f};
	half4 ret;

	half4 a = tex2D(SourceSampler, input.CentreUV - offset_y);
	half4 b = tex2D(SourceSampler, input.CentreUV);
	half4 c = tex2D(SourceSampler, input.CentreUV + offset_y);
	ret = (a+2*b+c)/4;

	return ret;
}


technique FixVideo
{
    pass P0
    {
		VertexShader = compile vs_1_1 VS_Video();
		// Choose which version to use here.
		PixelShader  = compile ps_2_0 PS_Video2();
		SRGBWRITEENABLE = FALSE;
    }  
}
